#ifndef IMAGEMANAGER_H
#define IMAGEMANAGER_H

#include <QObject>
#include <QPixmapCache>
#include <QDebug>
#include <QMutex>
#include <QFile>
#include <QBuffer>
#include <QDir>
#include "mapnetwork.h"

class MapNetwork;

class ImageManager : public QObject
{
    Q_OBJECT

public:

    static ImageManager* instance()
    {
        if(!m_Instance)
        {
            m_Instance = new ImageManager;
        }
        return m_Instance;
    }

    ImageManager(QObject* parent = 0);

    virtual ~ImageManager();

    QPixmap getImage(const QString& host, const QString& path);

    QPixmap prefetchImage(const QString& host, const QString& path);

    void receivedImage(const QPixmap pixmap, const QString& url);

    void loadingQueueEmpty();

    void abortLoading();

    void setProxy(QString host, int port);

    void setCacheDir(const QDir& path);

private:
    ImageManager(const ImageManager&);
    ImageManager& operator=(const ImageManager&);
    QPixmap emptyPixmap;
    MapNetwork* net;
    QVector<QString> prefetch;
    QDir cacheDir;
    bool doPersistentCaching;

    static ImageManager* m_Instance;

    bool saveTile(QString tileName,QPixmap tileData);
    bool loadTile(QString tileName,QPixmap &tileData);
    bool tileExist(QString tileName);

signals:
    void imageReceived();
    void loadingFinished();
};
#endif
